package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.PPayCustForm;
import com.shimao.celnet.payment.mapper.BEstateMapper;
import com.shimao.celnet.payment.mapper.PPayCustMapper;
import com.shimao.celnet.payment.model.PPayCust;
import com.shimao.celnet.payment.service.PPayCustService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.Map;


/**
 * 支付商户信息 管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class PPayCustServiceImpl extends BaseServiceImpl<PPayCust> implements PPayCustService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required = false)
    private BEstateMapper bEstateMapper;

    @Autowired(required = false)
    private PPayCustMapper pPayCustMapper;
    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public PPayCustForm getByPage(PPayCustForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(PPayCust.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(formBean.getEstateGuid())) {
            //物业小区
            criteria.andEqualTo("estateGuid", formBean.getEstateGuid());
        }
        if (StringUtil.isNotEmpty(formBean.getCustId())) {
            //商户id通联支付系统分配的支付商户id
            criteria.andLike("custId", "%" + formBean.getCustId() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getAppId())) {
            //应用id通联支付平台分配的APPID
            criteria.andLike("appId", "%" + formBean.getAppId() + "%");
        }

         //分页查询
        PageHelper.startPage(pageNum, length);
        List<PPayCust> tableList = super.getByExample(example);
        formBean.setPageInfo(tableList);

        //物业小区：列表取得
        formBean.setEstateList(bEstateMapper.selectAll());

        return  formBean;
    }

    /**
     * 根据id取得
     * @param pMap
     * @return
     */
    @Override
    public PPayCust getBySfGuid(Map<String ,String> pMap) {
        logger.info("分页查询--开始");
        Example example = new Example(PPayCust.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtil.isNotEmpty( pMap.get("custId"))){
            //商户id通联支付系统分配的支付商户id
            criteria.andEqualTo("custId",  pMap.get("custId") );
        }
        if (StringUtil.isNotEmpty( pMap.get("estateGuid"))){
            //小区guid
            criteria.andEqualTo("estateGuid",  pMap.get("estateGuid") );

        }
        if (StringUtil.isNotEmpty( pMap.get("appId"))){
            //应用id通联支付平台分配的APPID
            criteria.andEqualTo("appId",  pMap.get("appId") );
        }
        if (StringUtil.isNotEmpty( pMap.get("payType"))){
            //应用id通联支付平台分配的APPID
            criteria.andEqualTo("paytype",  pMap.get("payType") );
        }

        List<PPayCust> tableList = super.getByExample(example);
        if(tableList.size()>0){
            return tableList.get(0);
        }else{
            return  new PPayCust();
        }
    }

    /**
     * 根据id取得
     * @param
     * @return
     */
    @Override
    public PPayCust getBySfGuid1(String estateGuid ,String payType) {
        return pPayCustMapper.getBySfGuid1(estateGuid,payType);
    }

    /**
     * 根据商户号取得
     * @param
     * @return
     */
    @Override
    public PPayCust getByCustId(String custId) {
        return pPayCustMapper.getByCustId(custId);
    }

    /***
     * 保存对象
     * @param pPayCust
     */
    @Override
    public void saveObject(PPayCust pPayCust) {
        super.save(pPayCust);
    }

    /**
     *新增权限保存
     * @return
     */
    @Override
    public int save(PPayCust sBean){
        int retCnt=0;
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedUserGuid(loginUserGuid);
        sBean.setGuid(getGuid());//guid
        retCnt = super.save(sBean);

        return retCnt;
    }

    /**
     *修改权限保存
     * @return
     */
    @Override
    public int update(PPayCust sBean){
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        int retCnt = super.update(sBean);

        return retCnt;
    }

}
